package com.xm.event;

import java.util.EventObject;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 事件管理者（事件目标接口实现类）
 */
public class EventManager implements EventTarget<MyEventListener>{
    private final List<MyEventListener> listeners = new CopyOnWriteArrayList<>();

    @Override
    public void addListener(MyEventListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeListener(MyEventListener listener) {
        listeners.remove(listener);
    }

    @Override
    public void dispatchEvent(EventObject event) {
        if(!(event instanceof MyEvent)){
            throw new IllegalArgumentException("Unsupported event type");
        }
        for (MyEventListener listener : listeners) {
            listener.onEvent((MyEvent) event);
        }
    }
}
